<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/12 0012
 * Time: 11:09
 */

namespace app\admin\controller;


use think\Db;

class Message extends Middle
{
    //todo 消息列表
    public function MsgList(){
        $content = isset($this->data['content']) ? $this->data['content'] : '';
        $map = ['content'=>['like','%'.$content.'%']];
        $page = $this->page($this->data);
        $list = Db::name('message')
            ->where($map)
            ->limit($page['page'],$page['limit'])
            ->order('time desc')
            ->field('me_id,content,time as create_time,title')
            ->select();
        $count = Db::name('message')
            ->where($map)
            ->count();
        return $this->returnSuccess(['list'=>$list,'count'=>$count]);
    }
    //todo 消息发送
    public function MsgSend(){
        $uid = false;
        switch ($this->data['model']){
            case 1;
                $map = ['zn'=>1];
                $uid = Db::name('user_service')->where($map)->column('uid');
                break;
            case 2;
                $map = ['nx'=>1];
                $uid = Db::name('user_service')->where($map)->column('uid');
                break;
            case 3;
                $map = ['cl'=>1];
                $uid = Db::name('user_service')->where($map)->column('uid');
                break;
            case 5;
                $uid = Db::name('height_remind')->group('uid')->column('uid');
            break;
            case 6;
                $uid = Db::name('heyue_user')->group('uid')->column('uid');
                break;
            case 7;
                $uid = Db::name('beili_user')->group('uid')->column('uid');
                break;
        }
        if(!$uid)return $this->returnError('没有可发送的订阅用户');
        $mset = [
            'title' =>$this->data['title'],
            'content'=>$this->data['content'],
            'type'	=>$this->data['type'],
            'time'=>time(),
        ];
        Db::startTrans();
        try{
            $this->SendMsg($mset,$uid,$this->data['model']);
            $reg_id = $this->GetPushReg($uid);
            if($reg_id)$this->PushMsg($mset['content'],$mset['title'],$reg_id);
            Db::commit();
            return $this->returnSuccess(null,'发送成功');
        }catch (\Exception $e){
            Db::rollback();
            return $this->returnError($this->Error);
        }
    }
    //todo 消息模板列表
    public function MsgModel(){
        $page = $this->page($this->data);
        $content = isset($this->data['content']) ? $this->data['content'] : '';
        $type    = isset($this->data['type'])    ? $this->data['type']    : '';
        $map = ['content'   =>['like',['content'=>'%'.$content.'%']]];
        if($type == 9999)$map['type'] = ['not in',0];
        if($type == '1')$map['type'] = 0;
        $list = Db::name('message_model')
            ->limit($page['page'],$page['limit'])
            ->order('type asc')
            ->where($map)
            ->select();
        $count = Db::name('message_model')->where($map)->count();
        $list = ['list'=>$list,'count'=>$count];
        return $this->returnSuccess($list);
    }
    //todo 消息详情
    public function ModelDetail(){
        $map = ['id'=>$this->data['id']];
        $res = Db::name('message_model')->where($map)->find();
        if(!$res)return $this->returnError($this->NoData);
        return $this->returnSuccess($res);
    }
    //todo 消息设置
    public function ModelSet(){
        $set = [
            'title' =>$this->data['title'],
            'desc'  =>$this->data['desc'],
            'id'    =>$this->data['id'],
            'content'=>$this->data['content'],
        ];
        $res = Db::name('message_model')->update($set);
        if($res)return $this->returnSuccess(null,'修改成功');
        return $this->returnError($this->Error);
    }

    public function ModelDel(){
        $map  = ['id'=>$this->data['id']];
        $model = Db::name('message_model')->where($map)->find();
        if(!$model)return $this->returnError('该模板不存在');
        if($model['type'] != 0)return $this->returnError('不能删除自定义以外的模板');
        $res = Db::name('message_model')->where($map)->delete();
        if($res)return $this->returnSuccess(null,'操作成功');
        return $this->returnError($this->Error);
    }
}